package com.survey.project.research.controller;

import java.util.ArrayList;
import java.util.List;

import com.survey.project.research.domain.vo.ConsumerExcelVo;
import com.survey.project.research.domain.vo.ConsumerInVo;
import com.survey.project.research.domain.vo.ConsumerOutVo;
import com.survey.project.research.domain.vo.ResearchConsumerQuestionnaireDetailOutVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.survey.framework.aspectj.lang.annotation.Log;
import com.survey.framework.aspectj.lang.enums.BusinessType;
import com.survey.project.research.domain.Consumer;
import com.survey.project.research.service.IConsumerService;
import com.survey.framework.web.controller.BaseController;
import com.survey.framework.web.domain.AjaxResult;
import com.survey.common.utils.poi.ExcelUtil;
import com.survey.framework.web.page.TableDataInfo;

/**
 * 客户管理Controller
 *
 * @author survey
 * @date 2021-05-20
 */
@RestController
@RequestMapping("/research/consumer")
public class ConsumerController extends BaseController {
    @Autowired
    private IConsumerService consumerService;

    /**
     * 查询客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('research:consumer:list')")
    @GetMapping("/list")
    public TableDataInfo list(ConsumerInVo inVo) {
        startPage();
        List<ConsumerOutVo> list = consumerService.selectConsumerList(inVo);
        return getDataTable(list);
    }

    /**
     * 导出客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('research:consumer:export')")
    @Log(title = "客户管理" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ConsumerInVo inVo) {
        List<ConsumerOutVo> list = consumerService.selectConsumerList(inVo);
        List<ConsumerExcelVo> excelList = new ArrayList<>();
        for (ConsumerOutVo outVo : list) {
            ConsumerExcelVo excelVo = new ConsumerExcelVo();
            BeanUtils.copyProperties(outVo,excelVo);
            excelList.add(excelVo);
        }
        ExcelUtil<ConsumerExcelVo> util = new ExcelUtil<ConsumerExcelVo>(ConsumerExcelVo.class);
        return util.exportExcel(excelList, "consumer");
    }

    /**
     * 获取客户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('research:consumer:query')")
    @GetMapping(value = "/{consumerId}")
    public AjaxResult getInfo(@PathVariable("consumerId") Long consumerId) {
        return AjaxResult.success(consumerService.selectConsumerById(consumerId));
    }

    /**
     * 新增客户管理
     */
    @PreAuthorize("@ss.hasPermi('research:consumer:add')")
    @Log(title = "客户管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Consumer consumer) {
        return toAjax(consumerService.insertConsumer(consumer));
    }

    /**
     * 修改客户管理
     */
    @PreAuthorize("@ss.hasPermi('research:consumer:edit')")
    @Log(title = "客户管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Consumer consumer) {
        return toAjax(consumerService.updateConsumer(consumer));
    }

    /**
     * 删除客户管理
     */
    @PreAuthorize("@ss.hasPermi('research:consumer:remove')")
    @Log(title = "客户管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{consumerIds}")
    public AjaxResult remove(@PathVariable Long[] consumerIds) {
        return toAjax(consumerService.deleteConsumerByIds(consumerIds));
    }

    /**
     * 客户问卷详情
     */
    @PreAuthorize("@ss.hasPermi('research:consumer:questionnaire:detail')")
    @GetMapping(value = "/questionnaire/detail/{consumerId}")
    public TableDataInfo questionnaireDetail(@PathVariable("consumerId") Long consumerId) {
        List<ResearchConsumerQuestionnaireDetailOutVo> list = consumerService.questionnaireDetail(consumerId);
        return getDataTable(list);
    }
}
